package com.crazymaker.demo.drinkTea;

import lombok.extern.slf4j.Slf4j;
import rx.Observable;
import rx.functions.Func0;

@Slf4j
public class HotWaterJob
{

    private boolean waterOk;

    public void doHot() throws InterruptedException
    {
        log.info("洗好水壶" );
        log.info("灌上凉水" );
        log.info("放在火上" );

        //线程睡眠一段时间，代表烧水中
        Thread.sleep(500);
        log.info("水开了" );
        log.info("烧水工作，运行结束." );

        waterOk = true;

    }

    public Observable<Boolean> observable()
    {
        //        return Observable.defer(waterOk);
        return Observable.defer(new Func0<Observable<Boolean>>()
        {
            public Observable<Boolean> call()
            {

                try
                {
                    doHot();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                    log.info(" 发生异常被中断." );
                    waterOk = false;
                    return Observable.error(e);
                }
                return Observable.just(waterOk);
            }
        });

    }
}
